/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.request;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.inspur.edp.bff.api.extend.IBffEntityExtend;
import com.inspur.edp.bff.api.manager.IFSManager;
import com.inspur.edp.bff.spi.changeset.AbstractBffChangeJsonDeserializer;
import com.inspur.edp.bff.spi.utils.VoJsonDeserializer;
import com.inspur.edp.cef.api.manager.serialize.CefSerializeContext;
import com.inspur.edp.cef.api.request.RequestInfo;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.cef.variable.api.manager.IVariableManager;
import lombok.var;

import java.io.IOException;
import java.util.List;

public abstract class RequestConvertor extends VoJsonDeserializer<RequestInfo> {

    private static final String TitleDataChange = "dataChange";
    private static final String TitleVarChange = "variableChange";
    private CefSerializeContext context;
    private List<IBffEntityExtend> extList;

    protected abstract RequestInfo createRequsetInfo();

    protected abstract AbstractBffChangeJsonDeserializer getJsonConverter();

    protected abstract IFSManager createFsManager();

    @Override
    public RequestInfo deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {
        RequestInfo request = createRequsetInfo();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readValue(jsonParser, JsonNode.class);
        JsonNode changeToken = node.get(TitleDataChange);
        if (changeToken != null && changeToken.getNodeType() == JsonNodeType.ARRAY) {
            readDataChange(changeToken, request);
        }

        JsonNode varChangeToken = node.get(TitleVarChange);

        IVariableManager varManager = createFsManager().createVariableManager();
        if (varChangeToken != null && varManager != null) {
            IChangeDetail varChange = varManager.deserializeChange(varChangeToken.toString());
            request.setVariableChange(varChange);
        }

        return request;
    }

    private void readDataChange(JsonNode changeToken, RequestInfo request) throws IOException {


        for (JsonNode changeDetailToken : changeToken) {
            {
                IChangeDetail change = getChangeJsonMapper().readValue(changeDetailToken.toString(), IChangeDetail.class);
                request.getDataChange().add(change);
            }
        }
    }

    private ObjectMapper getChangeJsonMapper() {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        var convertor = getJsonConverter();
        if (extList != null) {
            extList.forEach(item -> convertor.addExtend(item));
        }
        convertor.setCefSerializeContext(context);
        module.addDeserializer(IChangeDetail.class, convertor);
        mapper.registerModule(module);
        return mapper;
    }

    public void setExtList(List<IBffEntityExtend> extList) {
        this.extList = extList;
    }

    public void setContext(CefSerializeContext context) {
        this.context = context;
    }

    /**
     * 表单运行时调用
     *
     * @param nodeCode 节点编号
     * @return CefResourceInfo
     */
    protected CefEntityResInfoImpl cefEntityResInfo(String nodeCode) {
        return (CefEntityResInfoImpl) createFsManager().getModelInfo().getCustomResource(nodeCode);
    }
}
